package com.xsy.carRental.controller;

import com.xsy.carRental.constant.MessageConstant;
import com.xsy.carRental.dto.*;
import com.xsy.carRental.entity.Rental;
import com.xsy.carRental.result.PageResult;
import com.xsy.carRental.result.Result;
import com.xsy.carRental.service.RentalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rental")
@Slf4j
public class RentalController {
    @Autowired
    private RentalService rentalService;

    /**
     * 新增订单
     *
     * @param rentalAddDTO
     * @return
     */
    @PostMapping()
    public Result AddRental(@RequestBody RentalAddDTO rentalAddDTO) {
        log.info("创建订单:{}", rentalAddDTO);
        rentalService.add(rentalAddDTO);

        return Result.success(MessageConstant.RENTALADD_SUCCESS);
    }


    /**
     * 分页查询订单
     *
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping
    public Result<PageResult> pageQuery(RentalPageQueryDTO rentalPageQueryDTO) {
        log.info("分页查询订单");
        PageResult pageResult = rentalService.pageQuery(rentalPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据可选条件分页查询订单
     *
     * @param searchQueryDTO
     * @return
     */
    @GetMapping("/search")
    public Result<PageResult> pageQuery(RentalSearchQueryDTO searchQueryDTO) {
        log.info("分页查询订单");
        PageResult pageResult = rentalService.SearchQuery(searchQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据订单id查询订单详细信息
     *
     * @param RentalId
     * @return
     */
    @GetMapping("/{RentalId}")
    public Result getRental(@PathVariable int RentalId) {
        log.info("根据订单id获取订单详细");
        Rental rental = rentalService.getRentalById(RentalId);

        return Result.success(rental);
    }

    /**
     * 修改订单详细信息
     *
     * @param rentalUpdateDTO
     * @return
     */
    @PutMapping("/{rentalId}")
    public Result updateRental(@PathVariable Integer rentalId, @RequestBody RentalUpdateDTO rentalUpdateDTO) {
        log.info("修改订单详细信息");
        rentalUpdateDTO.setRentalId(rentalId);
        rentalService.update(rentalUpdateDTO);
        return Result.success();
    }

    /**
     * 根据订单id删除订单
     *
     * @param rentalId
     */
    @DeleteMapping("/{rentalId}")
    public Result deleteRental(@PathVariable Integer rentalId) {
        log.info("删除id为{} 的订单", rentalId);
        rentalService.delete(rentalId);
        return Result.success();
    }


}
